package com.example.sms.service.impl;

import com.example.common.rabbit.service.RabbitService;
import com.example.common.redis.service.RedisService;
import com.example.common.util.constant.CommonUtilConstants;
import com.example.common.util.utils.RandomUtils;
import com.example.common.util.utils.StringUtils;
import com.example.model.vo.sms.SmsVo;
import com.example.service.util.service.MailService;
import com.example.service.util.service.MessageService;
import com.example.service.util.utils.MailTemplateUtils;
import com.example.sms.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author juzipi
 * @Date 2021/7/17 14:31
 * @Info
 */
@Service
public class SmsServiceImpl implements SmsService {


    @Autowired
    private MessageService messageService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private MailService mailService;



    @Override
    public Boolean sendCode(String phone) {
        //验证码6位
        String captcha = RandomUtils.captcha(6);
        //只存redis，不发送，由rabbit监听发送
//        Boolean aBoolean = messageService.sendCodeMessage(phone, captcha);
        //存入redis，验证码过期时间5分钟
        redisService.setCacheObject(phone,captcha,5L, TimeUnit.MINUTES);
        //发送rabbitmq消息
        SmsVo smsVo = new SmsVo();
        smsVo.setPhone(phone);
        smsVo.setTemplateCode(captcha);
        return rabbitService.sendMessage(CommonUtilConstants.SMS_EXCHANGE,CommonUtilConstants.SMS_ROUTING,smsVo);
    }


    //rabbit消息消费后调用此方法发送信息
    @Override
    public Boolean send(SmsVo smsVo) {
        String phone = smsVo.getPhone();
        String code = smsVo.getTemplateCode();
        if (StringUtils.isNotEmpty(phone)){
            //调用短信服务发送短信
            return messageService.sendCodeMessage(phone, code);
        }
        return false;
    }



    @Override
    public Boolean sendEmail(String subject,String template) {
        return mailService.sendRemindEmail("1124685815@qq.com", subject, template);
    }

}
